package com.mastercard.ganadores.struts.Form;

import com.mastercard.ganadores.util.to.userLogin;
import javax.servlet.http.HttpServletRequest;
import org.apache.struts.action.ActionErrors;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.action.ActionMessage;

public class loginForm extends ActionForm {
    
    private String user ;
    private String pass ;

    /**
     * @return the user
     */
    public String getUser() {
        return user;
    }

    /**
     * @param user the user to set
     */
    public void setUser(String user) {
        this.user = user;
    }

    /**
     * @return the pass
     */
    public String getPass() {
        return pass;
    }

    /**
     * @param pass the pass to set
     */
    public void setPass(String pass) {
        this.pass = pass;
    }
    
    public ActionErrors validate( ActionMapping mapping, HttpServletRequest request ) {
        
        ActionErrors errors = new ActionErrors( ) ;
        
        if( user == null || user.trim( ).length( ) == 0 ) {
            errors.add(" UsuarioVacio", new ActionMessage( "errors.login.incorrecto" ) ) ;
            return errors ;
        } else
            if( pass == null || pass.trim( ).length( ) == 0 ) {
                errors.add(" ClaveVacia", new ActionMessage( "errors.login.incorrecto" ) ) ;
                return errors ;
            }
        
        try {
            
            userLogin ul = new userLogin( user, pass ) ;
            
            boolean res = ul.login( ) ;
            
            if( !res ) {
                errors.add(" ClaveVacia", new ActionMessage( "errors.login.incorrecto" ) ) ;
                return errors ;
            } else
                request.getSession( true ).setAttribute( "UserLogin", ul ) ;
            
        } catch ( Exception e ) {
            e.printStackTrace( ) ;
        }
        
        return errors ;
    }
    
}
